﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.ComponentModel;
using tools;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    private string _pathToFile = string.Empty;
    private string _propertyName = string.Empty;
    private SchemaEditor _editor;
    private DoProperty _prop;

    protected void Page_Load(object sender, EventArgs e)
    {
        _errorPanel.Visible = false;
        _formPanel.Visible = true;

        if (!IsPostBack)
        {
            if (string.IsNullOrEmpty(Request["SDF_File"]) || string.IsNullOrEmpty(Request["Prop"]))
            {
                ShowErrorMessage("אין אפשרות לבצע פקודה");
                return;
            }
            _pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], Request["SDF_File"]);
            _propertyName = Request["Prop"];
            ViewState.Add("SDF_File", Request["SDF_File"]);
            ViewState.Add("Prop", _propertyName);

        }
        else
        {
            _pathToFile = Path.Combine(ConfigurationManager.AppSettings["SpatialDataPath"], ViewState["SDF_File"].ToString());
            _propertyName = ViewState["Prop"].ToString();
        }
        
        _editor = new SchemaEditor(_pathToFile);

        foreach (DoProperty prop in _editor.GetSchemaProperties().Where(x => x.Name == _propertyName))
        {
            if (prop.ValueConstraintsList == null && prop.ValueConstraintsList.Count == 0)
            {
                ShowErrorMessage("אין אפשרות לבצע פקודה לשדה המבוקש");
                return;
            }
            _prop = prop;
            break;
        }

        if (!IsPostBack)
        {
            _constraintsList.DataSource = _prop.ValueConstraintsList;
            _constraintsList.DataBind();
        }
    }

    protected void ShowErrorMessage(string message)
    {
        _errorMessage.Text = message;
        _errorPanel.Visible = true;
        _formPanel.Visible = false;
    }

    protected void AddNewConstraint(object sender, EventArgs e)
    {
        try
        {
            if (!string.IsNullOrEmpty(_txtNewConstraint.Text))
            {
                List<DoProperty> properties = _editor.GetSchemaProperties().ToList();
                foreach (var p in properties.Where(x => x.Name == _propertyName))
                {
                    if (!p.ValueConstraintsList.Contains(_txtNewConstraint.Text))
                        p.AddConstraint(_txtNewConstraint.Text);
                    _prop = p;
                    break;
                }
                _editor.ApplySchema(properties.ToArray());
                _txtNewConstraint.Text = string.Empty;
                _constraintsList.DataSource = _prop.ValueConstraintsList;
                _constraintsList.DataBind();
            }
        }
        catch (Exception ex)
        {
            ShowErrorMessage(ex.Message);
        }
    }
    protected void DeleteConstraint(object sender, EventArgs e)
    {
        try
        {
            if (_constraintsList.SelectedIndex != -1)
            {
                List<DoProperty> properties = _editor.GetSchemaProperties().ToList();
                foreach (var p in properties.Where(x => x.Name == _propertyName))
                {
                    if (p.ValueConstraintsList.Contains(_constraintsList.SelectedItem.Text))
                        p.DeleteConstraint(p.ValueConstraintsList.IndexOf(_constraintsList.SelectedItem.Text));
                    _prop = p;
                    break;
                }
                _editor.ApplySchema(properties.ToArray());
                _constraintsList.DataSource = _prop.ValueConstraintsList;
                _constraintsList.DataBind();
            }
        }
        catch (Exception ex)
        {
            ShowErrorMessage(ex.Message);
        }
    }
}
